<template>
	<view class="container">
		<view class="imgBox">
			<view v-for="(item,index) in contractImg" :key="index" class="contract">
				<easy-loadimage mode="widthFix" :scroll-top="scrollTop" :image-src="item"></easy-loadimage>
			</view>
		</view>
		<view class="veri" v-if="status == 0">
			<view class="cell">
				<text class="title">手机号码</text>
				<input class="input" disabled type="text" v-model="mobile" placeholder="请输入手机号码" placeholder-style="font-weight:normal;color:#bbb" /></view>
			<view class="cell">
				<text class="title">验证码</text>
				<input class="input" type="text" v-model="captcha" placeholder="请输入验证码" placeholder-style="font-weight:normal;color:#bbb" />
				<text class="veri" v-if="isCheck" @tap="yzm()">获取验证码</text>
				<text class="ver" v-else>{{ time }}秒后可再次获取</text>
			</view>
		</view>
		<view class="sign" v-if="status == 0">
			<view class="a" :class="captcha.length == 6?'':'disable'" @tap="captcha.length == 6&&sign()">电子签名</view>
			<text class="b">电子签名后即可提交审核</text>
		</view>
		<view class="bottom">
			<view class="rules" @tap="ruleCheck" v-if="status == 0">
				<image v-if="ruleChecked" src="../../../static/images/center/xuanzhong.png" mode=""></image>
				<view class="gray" v-else></view>
				<view class="text"><text>同意</text><text @tap="goRules(25,'提货卡购买协议')">《提货卡购买协议》</text></view>
			</view>
			<!-- <view v-if="status == 0" @tap="sub()" class="sub">提交审核</view> -->
			<view v-if="status == 1" class="sub btnGray">审核中</view>
			<!-- #ifdef APP-PLUS -->
			<view v-if="status == 2" @tap="down()" class="sub">发送合同到邮箱</view>
			<!-- #endif -->
			<!-- #ifdef H5 -->
			<view v-if="status == 2" @tap="down()" class="sub">下载合同</view>
			<!-- #endif -->
		</view>
		<uni-popup ref="uniPopup" type="center">
			<view class="box">
				<image @tap="close" src="../../../static/images/center/close.png" mode=""></image>
				<view class="title">邮箱地址</view>
				<view class="content">
					<view class="inputBox">
						<input type="text" v-model="email" placeholder="请填写邮箱地址" />
					</view>
					<view @tap="sendEmail" class="boxbtn">确认</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		deliveryExecuteFile,
		takeDeliverySendMail,
		takeSendCode
	} from "@/api/user.js"
	export default {
		data() {
			return {
				status: 0,
				type: 0,
				orderId: "",
				ruleChecked: true,
				contractImg: [],
				fileId: 0,
				dstFilePath: "",
				isCheck: true,
				time: 60,
				mobile: "",
				captcha: "",
				email: "",
				scrollTop: 0
			}
		},
		onLoad(option) {
			this.type = option.type
			this.orderId = option.orderId
			if(this.type == 2) {
				uni.setNavigationBarTitle({
					title:"提货卡收购合同"
				})
			}
		},
		onShow() {
			uni.showLoading({
				mask: true,
				title:"合同生成中..."
			})
			this.getCon()
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop
		},
		onPullDownRefresh() {
			this.getCon()
		},
		methods: {
			getCon() {
				deliveryExecuteFile({
					order_id: this.orderId,
					file_type: this.type
				}).then(res => {
					setTimeout(function() {
						uni.stopPullDownRefresh();
					}, 1000);
					if (res.data.status) {
						uni.showLoading({
							mask: true,
							title:"合同生成中..."
						})
						let that = this
						setTimeout(function() {
							that.getCon()
						},10000)
					} else {
						this.contractImg = res.data.fileInfo.fileImag
						this.status = res.data.fileInfo.status
						this.fileId = res.data.fileInfo.fileId
						this.dstFilePath = res.data.fileInfo.dstFilePath
						this.mobile = res.data.fileInfo.user_phone
					}
				})
			},
			sign() {
				uni.navigateTo({
					url: "./pickCardSign?fileId=" + this.fileId + "&captcha=" + this.captcha
				})
			},
			ruleCheck() {
				this.ruleChecked = !this.ruleChecked
			},
			down() {
				//#ifdef H5
				window.location.href = this.dstFilePath
				//#endif
				// #ifdef APP-PLUS
				this.$refs.uniPopup.open()
				//#endif
			},
			close() {
				this.$refs.uniPopup.close()
			},
			yzm() {
				uni.showLoading({
					mask:true
				})
				takeSendCode({
					order_id: this.orderId,
					type: this.type
				}).then(res => {
					uni.hideLoading()
					uni.showToast({
						icon: "none",
						title: res.msg
					});
					this.isCheck = false;
					this.countdown();
				})
			},
			countdown() {
				let _self = this;
				if (_self.time == 0) {
					_self.isCheck = true;
					_self.time = 60;
				} else {
					_self.time--;
					setTimeout(_self.countdown, 1000);
				}
			},
			sendEmail() {
				uni.showLoading({
					mask: true
				})
				takeDeliverySendMail({
					order_id: this.orderId,
					type: this.type,
					email: this.email
				}).then(res => {
					uni.hideLoading()
					this.close()
					uni.showToast({
						title: res.msg
					})
				})
			}
		}
	}
</script>
<style lang="scss" scoped>
	.container {
		.imgBox {
			min-height: 100vh;
		}

		.contract {
			width: 100%;
			height: auto;
		}
		.veri{
			border-top: 20rpx solid #fafafa;
			padding:20rpx 100rpx 50rpx;
			background: #fff;
			
			.cell {
				display: flex;
				border-bottom: 1rpx solid #eee;
				position: relative;
			
				.title {
					height: 100rpx;
					line-height: 100rpx;
					min-width: 150rpx;
					font-size: 30rpx;
					color: #666666;
				}
			
				.input {
					font-size: 30rpx;
					color: #333;
					font-weight: bold;
					height: 100rpx;
					line-height: 100rpx;
				}
			
				.veri {
					position: absolute;
					right: 0;
					top: 28rpx;
					font-size: 28rpx;
					color: #57BE6A;
					padding: 2rpx 10rpx;
					border: 2rpx solid #57BE6A;
					border-radius: 4rpx;
				}
			
				.ver {
					position: absolute;
					right: 0;
					top: 32rpx;
					font-size: 24rpx;
					color: #bbb;
					padding: 2rpx 10rpx;
				}
			}
		}
		
		.sign {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 0 0 50rpx;

			.a {
				width: 160rpx;
				height: 60rpx;
				background: #57BE6A;
				border-radius: 8rpx;
				font-size: 30rpx;
				color: #FFFFFF;
				line-height: 60rpx;
				text-align: center;
			}
			.disable{
				background: #ddd;
			}
			.b {
				margin-top: 10rpx;
				font-size: 28rpx;
				color: #999999;
			}
			
		}
		
		.bottom {
			padding: 25px 0;
			background: #FAFAFA;
			display: flex;
			flex-direction: column;
			align-items: center;

			.rules {
				width: 100%;
				display: flex;
				justify-content: center;
				align-items: center;

				image {
					margin-right: 14rpx;
					width: 30rpx;
					height: 30rpx;
					border-radius: 20rpx;
				}

				.gray {
					margin-right: 14rpx;
					box-sizing: border-box;
					border: 2rpx #999 solid;
					width: 30rpx;
					height: 30rpx;
					border-radius: 20rpx;
				}

				.text {
					font-size: 24rpx;
					color: #333;

					text:nth-of-type(2) {
						color: #FACF3C;
					}
				}
			}

			.sub {
				margin-top: 20px;
				width: 80%;
				height: 40px;
				background: linear-gradient(90deg, #FBCC2B 0%, #FBDC72 100%);
				border-radius: 20px;
				text-align: center;
				font-size: 16px;
				font-weight: bold;
				color: #333333;
				line-height: 40px;
			}

			.btnGray {
				color: #bbb;
				background-color: #FBDC72;
			}

			.contact {
				display: flex;
				justify-content: center;
				padding: 30rpx;

				image {
					width: 40rpx;
					height: 41rpx;
					margin-right: 14rpx;
				}

				text {
					font-size: 32rpx;
					color: #666;
				}
			}
		}

		.box {
			background-color: #fff;
			position: relative;
			width: 600rpx;
			padding: 36rpx;
			box-sizing: border-box;
			border-radius: 10rpx;

			image {
				position: absolute;
				top: 0rpx;
				right: 0rpx;
				width: 26rpx;
				height: 26rpx;
				padding: 30rpx;
			}

			.title {
				text-align: center;
				font-size: 40rpx;
				font-weight: bold;
			}

			.content {

				.inputBox {
					position: relative;
					padding-top: 14rpx;
					height: 100rpx;
					line-height: 100rpx;
					border-bottom: 1rpx solid #eee;

					input {
						height: 100rpx;
						font-size: 36rpx;
					}
				}

				.boxbtn {
					margin: 40rpx 0 0;
					width: 528rpx;
					height: 80rpx;
					background: #FBC866;
					border-radius: 40rpx;
					color: #333;
					font-weight: bold;
					line-height: 80rpx;
					text-align: center;
				}
			}

		}
	}
</style>
